package ualrcdg.client;

import ualrcdg.shared.CDGXMLHelper;
import ualrcdg.shared.exceptions.CDGException;
import ualrcdg.shared.requests.RegisterPlayerRequest;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * This class allows the user to create a new account with CDG. <br />
 * Continues from {@link ProfileAccesssorActivity} <br />
 * Continues to {@link LoginActivity}
 */
public class CreateProfileActivity extends Activity
{
	private CommunicatorService mBoundService;
    private Boolean mIsBound;
    public CreateProfileActivity cpa;

	/**
	 * This method creates the UI when executed.
	 * 
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        cpa = this;
        setContentView(R.layout.createprofilelayout);
        final CommunicatorService communique = new CommunicatorService();
        
        final CreateProfileActivity pc = this;
        final EditText UsernameField = (EditText) findViewById(R.id.EditUsername);
        final EditText PasswordField = (EditText) findViewById(R.id.EditPassword);
        final EditText ConfirmationField = (EditText) findViewById(R.id.EditConfirmation);
        final Button YesButton = (Button) findViewById(R.id.button1);
        
        YesButton.setOnClickListener(new View.OnClickListener()
        {
        	/**
        	 * This method is called when the button on the interface is pressed.
        	 * When executed this method will register the user data with the CDG
        	 * server and launch the next activity.
        	 * 
        	 * (non-Javadoc)
        	 * @see android.view.View.OnClickListener#onClick(android.view.View)
        	 */
             public void onClick(View v)
             {
            	 startService(new Intent(CreateProfileActivity.this,CommunicatorService.class));
                 doBindService(); 
            	 String Username = UsernameField.getText().toString();
            	 String Password = PasswordField.getText().toString();
            	 String Confirmation = ConfirmationField.getText().toString();
            	 
            	 if(Password.equals(Confirmation))
            	 {
            		 communique.makeConnection(CDGXMLHelper.createRegisterPlayerRequestDocument(new RegisterPlayerRequest("0", Username, Password)));
            		 startActivity(new Intent(pc, ualrcdg.client.LoginActivity.class).putExtra("Username", Username).putExtra("Password", Password));
            	 }
            	 else
                 {
                 	Toast.makeText(pc, "Your password does not match your confirmation!", Toast.LENGTH_LONG).show();
                 }
             }
         });  
    }
	
	private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mBoundService = ((CommunicatorService.LocalBinder)service).getService();

        }
        public void onServiceDisconnected(ComponentName className) {
            mBoundService = null;
        }
    };

    private void doBindService() {
        bindService(new Intent(CreateProfileActivity.this, CommunicatorService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
        mBoundService.IsBoundable();
    }


    private void doUnbindService() {
        if (mIsBound) {
            // Detach our existing connection.
            unbindService(mConnection);
            mIsBound = false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        doUnbindService();
    }
}
